package com.wteam.dragon.shop.search.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.dragon.shop.mapper.EsProductAttributeValueMapper;
import com.wteam.dragon.shop.mapper.EsProductMapper;
import com.wteam.dragon.shop.mapper.ProductAttributeMapper;
import com.wteam.dragon.shop.pojo.bean.ProductAttribute;
import com.wteam.dragon.shop.search.bean.EsProduct;
import com.wteam.dragon.shop.search.bean.EsProductAttributeValue;
import com.wteam.dragon.shop.search.repository.EsProductRepository;
import com.wteam.dragon.shop.search.service.ProductSearch;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/27 8:06
 */
@Component
@RequiredArgsConstructor
public class ProductSearchImpl implements ProductSearch {
    private final EsProductMapper esProductMapper;
    private final EsProductRepository productRepository;
    private final ElasticsearchRestTemplate elasticsearchRestTemplate;
    private final EsProductAttributeValueMapper esProductAttributeValueMapper;
    private final ProductAttributeMapper productAttributeMapper;

    @Override
    public ResultMessage importAll() {
        List<EsProduct> esProductList = esProductMapper.selectList(null);
        esProductList.forEach(this::dealWithEsProduct);
        productRepository.saveAll(esProductList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    public ResultMessage insert(Long id) {
        EsProduct esProduct = esProductMapper.selectById(id);
        if(esProduct == null){
            throw new BadRequestException("未找到该商品");
        }
        dealWithEsProduct(esProduct);
        productRepository.save(esProduct);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    private void dealWithEsProduct(EsProduct esProduct){
        QueryWrapper<EsProductAttributeValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(EsProductAttributeValue::getProductId,esProduct.getId());
        List<EsProductAttributeValue> esProductAttributeValueList = esProductAttributeValueMapper.selectList(queryWrapper);
        esProductAttributeValueList.forEach(esProductAttributeValue -> {
            QueryWrapper<ProductAttribute> productAttributeQueryWrapper = new QueryWrapper<>();
            productAttributeQueryWrapper.lambda()
                    .select(ProductAttribute::getName, ProductAttribute::getType)
                    .eq(ProductAttribute::getId, esProductAttributeValue.getProductAttributeId());
            ProductAttribute productAttribute = productAttributeMapper.selectOne(productAttributeQueryWrapper);
            esProductAttributeValue.setName(productAttribute.getName());
            esProductAttributeValue.setType(productAttribute.getType());
        });
        esProduct.setAttrValueList(esProductAttributeValueList);
    }

    @Override
    public ResultMessage deleteById(Long id) {
        productRepository.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    public ResultMessage deleteByIds(List<Long> ids) {
        List<EsProduct> list = new ArrayList<>();
        ids.forEach(id -> {
            EsProduct esProduct = new EsProduct();
            esProduct.setId(id);
            list.add(esProduct);
        });
        productRepository.deleteAll(list);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    public ResultMessage searchByKeyword(String keywords, Integer current, Integer size) {
        Pageable pageable = PageRequest.of(current, size);
        Page<EsProduct> esProductPage = productRepository.findByNameOrSubheadOrKeywords(keywords, keywords, keywords, pageable);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("esProductPage", esProductPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }


}
